//
//  MyVoucherViewController.m
//  ChunLaiZhuangClient
//
//  Created by zouzhisheng on 16/3/4.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "MyVoucherViewController.h"
#import "CustomVoucherTableViewCell.h"
#import "CouponListModel.h"
#import "CouponEmptyView.h" 
#import "CLZRefreshHeader.h"

@interface MyVoucherViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong) NSMutableArray *couponArray;
@property(nonatomic, strong) CouponEmptyView *emptyView;
@end

@implementation MyVoucherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNaviTitle:@"我的优惠券"];
    [self setNaviLeftButton:@""];
    [self.view addSubview:self.displayView];
    __weak typeof(self) weakSelf = self;
    [self getCouponList];
    self.displayView.mj_header =  [CLZRefreshHeader headerWithRefreshingBlock:^{
        [weakSelf getCouponList];
    }];
    self.view.backgroundColor = [UIColor whiteColor];
    self.emptyView.hidden = YES;
    self.couponArray=[[NSMutableArray alloc] init];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CouponEmptyView *)emptyView {
    if (!_emptyView) {
        _emptyView = [[CouponEmptyView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.width)];
        [self.view addSubview:_emptyView];
        [_emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.top.equalTo(@(10));
            make.size.mas_equalTo(CGSizeMake(300, 300));
        }];
    }
    return _emptyView;
}

-(UITableView*)displayView{
    if (!_displayView) {
        _displayView=[[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
        _displayView.delegate=self;
        _displayView.dataSource=self;
        _displayView.backgroundColor=[UIColor clearColor];
        _displayView.tableFooterView=[[UIView alloc] initWithFrame:CGRectZero];
        // [_displayView setSeparatorColor:[UIColor redColor]];
    }
    return _displayView;
}

- (void)getCouponList {
    [[LKDHud shareHud] showLoadingView];
    [ApiClient getCouponListFinishLoaded:^(CouponListModel *model, NSString *path) {
        [[LKDHud shareHud] hideLoadingView];
        [self.displayView.mj_header endRefreshing];
        if ([model.status isEqual:@(1)]) {
            if(model.data.coupons.count > 0) {
                [self.couponArray removeAllObjects];
                for(CouponModel *couponmodel in model.data.coupons){
                    if (couponmodel.valid) {
                        [self.couponArray addObject:couponmodel];
                    }
                }
                [self.displayView reloadData];
                self.emptyView.hidden = YES;
            } else {
                self.emptyView.hidden = NO;
            }
        }
    } didFailLoaded:^(NSError *error, NSString *path) {
        [self.displayView.mj_header endRefreshing];
        [[LKDHud shareHud] hideLoadingView];
    }];
}

#pragma mark-UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.couponArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//            QLKEmptyCellTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:kEmptyCellTableViewCellIdentify];
//            if (!cell) {
//                cell = [[QLKEmptyCellTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kEmptyCellTableViewCellIdentify];
//            }
//    
//            [cell setLogoImage: @"sign_youhui" cellHeight:self.displayView.height];
//            cell.titleLabel.text = @"您还没有优惠券" ;
//            cell.selectionStyle=UITableViewCellSelectionStyleNone;
//            return cell;

    static NSString *identifier = @"CustomVoucherTableViewCell";
    CustomVoucherTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                              identifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomVoucherTableViewCell"  owner:self options:nil] lastObject];
        
    }
    CouponModel *coupon = [self.couponArray objectAtIndex:indexPath.row];
    [cell updateCouponInfo:coupon];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    return self.displayView.height;
    return 93;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
